package cn.webrx.controller;

import cn.webrx.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.Arrays;
import java.util.Map;
@SessionAttributes(value={"names"},types={Integer.class})
@Controller
public class IndexController {
    @Autowired
    private BookMapper bookMapper;


    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("books", bookMapper.queryAll());
        model.addAttribute("book", bookMapper.queryById(103));
        return "index/index";
    }

    @ModelAttribute
    public void m1() {
        System.out.println("m1");
    }

    @ModelAttribute
    public void m2() {
        System.out.println("m2");
    }

    @ModelAttribute
    public String m3() {
        //Model.put('string','helloworld')
        return "helloworld";
    }

    @ModelAttribute("hello")
    public String m4() {
        //Model.put('hello','helloworldmodelattribute'
        return "helloworldmodelattribute";
    }

    @RequestMapping("/test")
    public String test(Map<String, Object> map) {
        map.put("names", Arrays.asList("张三", "李四", "王五"));
        map.put("age", 18);
        return "index/test";
    }
}